#####################################################################
# Product:      KnowledgeEngine (Plone Edition)
#
# File:         Metadata.py
#
# Copyright:    Copyright (c) 2004, Faulkner Technologies
#
# Author:       Brian Oliver
#
# Description:  This file defines the Metadata class, a simple
#               Button for placement in Toolbars
#
# See Also:     Element, Toolbar
#####################################################################
#####################################################################
## Knowledge Engine Library Imports
from Products.KnowledgeEngine.Common import *
from Products.KnowledgeEngine.Core import *
from Products.KnowledgeEngine.Elements.TabBar.TabBarTool import TabBarTool, TabBarToolInfo
from Products.KnowledgeEngine.PropertyDefinition import PropertyDefinition
from Products.KnowledgeEngine.PropertyFormats import *
from Products.KnowledgeEngine.PropertyTypes import *
from Products.KnowledgeEngine.PropertyDefinition import *
from Products.KnowledgeEngine.PropertyTypes import *
from Products.KnowledgeEngine.PropertyFormats import *
from Products.KnowledgeEngine.PropertyStructures import *

from Products.KnowledgeEngine.Components.Section.Section import Section, SectionInfo

#####################################################################
## Zope Library Imports
from Globals import DTMLFile
from App.ImageFile import ImageFile

#####################################################################
## Python Library Imports
# (none)

#####################################################################
## Private Functions (global)

def getPages(context):
    """
        Returns a [(name,identity)] of pages within the specified
        context (model).
    """
    #get our model
    model = context.getParentByInstanceOf("Model")
    #return the list of available pages in the model
    return [(page.getTitleOrId(),page.getIdentity()) for page in model.getPages()]

#####################################################################
## MetadataInfo Class Definition
class MetadataSectionInfo(SectionInfo):
    """
        Defines the class info methods, defining information about
        the class without needing an instance of the class
    """
    
    #---------------------------------------------------------------#
    
    def getClassName(self):
        """
            Returns the name of the class for which 
            we are providing information.
        """
        return "MetadataSection"

    #---------------------------------------------------------------#
    
    def isInstanceOf(self, className):
        
        return className in ('MetadataSection','Section') or SectionInfo.isInstanceOf(self, className)
    
    #---------------------------------------------------------------#

    def getDisplayName(self):
        """
            Return a friendly name for the class instances
            (this is used for displaying in lists etc)
        """
    
        return "Metadata Section"
        
    #---------------------------------------------------------------#

    def getIdentityType(self):
        """
            Return the identity type.  
        """
        
        return "mdsec"
        
    #---------------------------------------------------------------#
    
    def getClassIcon(self):
        """
            Return the appropriate icon file for the class.
        """
        
        return ImageFile('skins/MetadataSection_icon.gif', globals())
        
    #---------------------------------------------------------------#

    def isPermittedInContainer(self, containerClassInfo, containerInstance = None, instance=None):
        """
            ABSTRACT: Returns if class instances (instance) is permitted in 
            the specified container (containerInstance)
            
            NOTE: This is from this class' instance perspective.
        """
        #DEFAULT RULE: Metadatas are only permitted in TabBars and Models
        return containerClassInfo.isInstanceOf('Model') or containerClassInfo.isInstanceOf('Section')
            
    #---------------------------------------------------------------#

    def onConfigureProperties(self):
        """
            Automatically called by the Knowledge Engine once
            the class has been loaded.  Use this method to register 
            class specific properties using the PropertyReflection interface.
            
            If this class is sub-classed, a call must be made to
            the super-class to ensure super-class properties are
            correctly registered
            
            NOTE: This method is called before ClassInfo.onAfterStartup()            
        """

        #initialise the super-class properties for reflection
        SectionInfo.onConfigureProperties(self)

        #set the default title
        self.getPropertyDefinition('title').setDefaultValue('Metadata')

        #set the default id
        self.getPropertyDefinition('id').setDefaultValue('mdsec')
      

    #---------------------------------------------------------------#

#####################################################################
## Metadata Class Definition
class MetadataSection(Section):
    """
        A class to represent a button in a toolbar
    """

    #---------------------------------------------------------------#

    #ABSTRACT TRANSIENT: a ClassInfo implementation instance to provide info about this class
    _CLASSINFO = MetadataSectionInfo()

    #---------------------------------------------------------------#

    def __init__(self, id, identity):
        "Constructor for the Metadata Section"        

        Section.__init__(self, id, identity)
                
        #perform onAfterInstanciation processing
        self.onAfterInstanciation()


    #---------------------------------------------------------------#
                
    def getIsShownInPageList(self, context=None):
        """
            Is this section hidden from page lists?
        """
        
        # Only show in the nav dropdown if we are the currently active page
        # TODO: Make this an option?
        return context == self and true or false
        


        
    #---------------------------------------------------------------#

#####################################################################
## Class Initialisation

# Register Class Information with the Knowledge Engine
registerGenericObjectClass(MetadataSection)

#####################################################################

